import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const pinia = createPinia()
// 持久化
pinia.use(piniaPluginPersistedstate)

import type { PersistedStateOptions } from 'pinia-plugin-persistedstate'

/**
 * @description pinia持久化参数配置
 * @param {String} key 存储到持久化的 name
 * @param {Array} paths 需要持久化的 state name
 * @return persist
 * */
export const piniaPersistConfig = (
  key: string,
  paths?: string[],
  storage = window.localStorage
) => {
  const persist: PersistedStateOptions = {
    key,
    storage,
    // storage: window.sessionStorage,
    paths
  }
  return persist
}

export default pinia
